package com.zoe.thread.th06_集合不安全问题;

import cn.hutool.core.lang.UUID;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author Zoe
 * @Date: 2021/05/03
 * @Description
 */
public class HashSetDemo {
    public static void main(String[] args) {
        setSafe();
    }

    private static void setSafe() {
//        Set<String> sets = new HashSet();
//        Set<String> sets = Collections.synchronizedSet(new HashSet());
        Set<String> sets = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 300; i++) {
            new Thread(() -> {
                sets.add(UUID.randomUUID().toString().replace("-", "").substring(0, 5));
                System.out.println(sets);
            }, i + "").start();
        }
    }
}
